/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entity.Employee;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
 *
 * @author JAVIER
 */
public class EmployeeDAO implements DAO <Employee> {
    
    private Integer ID = 0;  
        
    private static final String SQL_INSERT = " INSERT INTO Employee (" +
"			 name, login, password, idNumber, idType" +
"             ) VALUES ( ?, ?, ?,?, ?)";
    
    private static final String SQL_SELECT = " SELECT * " +
"               FROM Employee where id= ?";
    
    private static final String SQL_SELECT_PROPERTY = " SELECT * FROM Employee where ? = ?" ;
    
    private static final String SQL_SELECT_All = " SELECT * " +
"             FROM Employee";
    
    private static final String SQL_UPDATE = " UPDATE Employee SET " +
"             id =? , name = ?, login = ?, password = ?, idNumber = ?, idType = ?" +
"              WHERE " +
"             id = ? ";

    private static final String SQL_DELETE = " DELETE FROM Employee WHERE " +
"             id = ?";
    
    Integer create(Employee object, Connection connect) throws SQLException {

        PreparedStatement ps = null;
        try {
            ps = connect.prepareStatement(SQL_INSERT, Statement.RETURN_GENERATED_KEYS);
            //ps.setInt(1, Client.getId());
            ps.setString(1, Employee.getName());
            ps.setString(2, Employee.getLogin());
            ps.setString(3,Employee.getPassword());
            ps.setString(4,Employee.getIdNum());
            ps.setString(5,Employee.getIdType());

            ID = ps.executeUpdate();
        } finally {
            close(ps);
        }
        return ID;
        //TODO implement
    }
    
    Boolean remove(Employee object, Connection connect) throws SQLException {
        Boolean result= false;
        PreparedStatement ps = null;
        try {
            ps = connect.prepareStatement(SQL_DELETE);
            ps.setInt(1, Employee.getId());
            ps.executeUpdate();
            result = true;
        } finally {
            close(ps);
        }
        return result;
        //TODO implement
    }
    
    Boolean update(Employee object, Connection connect) throws SQLException {
        PreparedStatement ps = null;
        try {
            ps = connect.prepareStatement(SQL_UPDATE,Statement.RETURN_GENERATED_KEYS);
            ps.setInt(1, Employee.getId());
            ps.setString(2, Employee.getName());
            ps.setString(3, Employee.getLogin());
            ps.setString(4,Employee.getPassword());
            ps.setString(5,Employee.getIdNum());
            ps.setString(6,Employee.getIdType());
            ps.setInt(7, Employee.getId());
            ID = ps.executeUpdate();
            
        } finally {
            close(ps);
        }
        
        return ID;
        //TODO implement
    }
    
    Employee find(Integer id, Connection connect) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connect.prepareStatement(SQL_SELECT);
            ps.setString(1, id);
            rs = ps.executeQuery();
            Employee emp = new Employee();
            result = null;
            emp = get_employe(rs);
            return emp;
        } finally {
            close(rs);
            close(ps);  
        }
        //TODO implement
    }
    
    List findAll(Connection connect) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connect.prepareStatement(SQL_SELECT_All);
            rs = ps.executeQuery();
            List results = new ArrayList();
            Employee emp = new Employee();
            result = null;
            while (rs.next()) {
                emp = get_employe(rs);
                results.add(emp);}
            return result;
        } finally {
            close(rs);
            close(ps);  
        }
        //TODO implement
    }
    
    List select_by_property(String camp, String Value, Connection connect)throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connect.prepareStatement(SQL_SELECT_PROPERTY);
            ps.setString(1, camp);
            ps.setString(2, value);
            rs = ps.executeQuery();
            List results = new ArrayList();
            Employee emp = new Employee();
            result = null;
            while (rs.next()) {
                emp = get_employe(rs);
                results.add(emp);}
            return result;

        } finally {
            close(rs);
            close(ps);  
        }
        //TODO implement
    }
        
    Employee get_employe (ResultSet rs) throws SQLException {
 
        Employee emp = new Employee();
        emp.setId(rs.getInt("id"));
        emp.setName(rs.getString("name"));
        emp.setLogin(rs.getString("login"));
        emp.setPassword(rs.getString("password"));
        emp.setIdNum(rs.getString("idNumber"));
        emp.setIdType(rs.getString("idType"));

        return emp;
    }
    
}
